package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.ACT;
import Models.Allocation;

public class AllocationPresenter extends AbstractPresenter<Allocation> {
	final String SINGLEITEM = "allocation";
	final String MULTIITEM = "allocations";
	
	public ArrayList<Allocation> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Allocation tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Allocation createItemFromJSONObject(JSONObject jsonObject){
		Allocation item = new Allocation();
		try{
			item.setAllocationDueDate(getDateFromString(jsonObject.getString("allocationDueDate")));
			item.setAllocationFilled(jsonObject.getBoolean("allocationFilled"));
			item.setAllocationID(jsonObject.getInt("allocationID"));
			item.setAllocationModDate(getDateFromString(jsonObject.getString("allocationModDate")));
			item.setAllocationStartDate(getDateFromString(jsonObject.getString("allocationStartDate")));
			item.setTemplateID(jsonObject.getInt("templateID"));
			
			CustomerGroupPresenter custPresenter = new CustomerGroupPresenter();
			JSONObject jsonFGI = jsonObject.getJSONObject("allocationCustGroupID");
			item.setCustGroup(custPresenter.createItemFromJSONObject(jsonFGI));
			
			//Set LabType Collection
			ArrayList<ACT> customers = new ArrayList<ACT>();
			if(!jsonObject.isNull("ACTCollection")){
				Object itm = jsonObject.get("ACTCollection");
				ACTPresenter presenter = new ACTPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						customers.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					customers.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setActs(customers);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Allocation item){
		try{
			JSONObject json = new JSONObject();
			json.put("allocationDueDate", getDate(item.getAllocationDueDate()));
			json.put("allocationFilled", item.isAllocationFilled());
			json.put("allocationID", item.getAllocationID());
			json.put("allocationModDate", getDate(item.getAllocationModDate()));
			json.put("allocationStartDate", getDate(item.getAllocationStartDate()));
			json.put("templateID", item.getTemplateID());
			
			CustomerGroupPresenter custPresenter = new CustomerGroupPresenter();
			json.put("allocationCustGroupID", custPresenter.createJSON(item.getCustGroup()));
			
			//Create Beers Collection
			if(item.getActs()!=null){
				ACTPresenter presenter = new ACTPresenter();
				if(item.getActs().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getActs().size(); i++){
						jsonArray.put(presenter.createJSON(item.getActs().get(i)));
					}
					json.put("ACTCollection", jsonArray);
				}
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Allocation getEmptyEntity(){
		Allocation entity = new Allocation();
		return entity;
	}
}
